package com.ruoyi.sywx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sywx.domain.bo.SywxYqAnnexBo;
import com.ruoyi.sywx.domain.vo.SywxYqAnnexVo;
import com.ruoyi.sywx.domain.SywxYqAnnex;
import com.ruoyi.sywx.mapper.SywxYqAnnexMapper;
import com.ruoyi.sywx.service.ISywxYqAnnexService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 舆情附件Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-09
 */
@RequiredArgsConstructor
@Service
public class SywxYqAnnexServiceImpl implements ISywxYqAnnexService {

    private final SywxYqAnnexMapper baseMapper;

    /**
     * 查询舆情附件
     */
    @Override
    public SywxYqAnnexVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询舆情附件列表
     */
    @Override
    public TableDataInfo<SywxYqAnnexVo> queryPageList(SywxYqAnnexBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SywxYqAnnex> lqw = buildQueryWrapper(bo);
        Page<SywxYqAnnexVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询舆情附件列表
     */
    @Override
    public List<SywxYqAnnexVo> queryList(SywxYqAnnexBo bo) {
        LambdaQueryWrapper<SywxYqAnnex> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SywxYqAnnex> buildQueryWrapper(SywxYqAnnexBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SywxYqAnnex> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getYqId() != null, SywxYqAnnex::getYqId, bo.getYqId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SywxYqAnnex::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), SywxYqAnnex::getUrl, bo.getUrl());
        return lqw;
    }

    /**
     * 新增舆情附件
     */
    @Override
    public Boolean insertByBo(SywxYqAnnexBo bo) {
        SywxYqAnnex add = BeanUtil.toBean(bo, SywxYqAnnex.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean insertBatchByBoList(List<SywxYqAnnexBo> boList) {
        List<SywxYqAnnex> list = BeanUtil.copyToList(boList, SywxYqAnnex.class);
        return baseMapper.insertBatch(list);
    }

    /**
     * 修改舆情附件
     */
    @Override
    public Boolean updateByBo(SywxYqAnnexBo bo) {
        SywxYqAnnex update = BeanUtil.toBean(bo, SywxYqAnnex.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SywxYqAnnex entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除舆情附件
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<SywxYqAnnex> queryByYqId(Long id) {
        return baseMapper.selectList(Wrappers.lambdaQuery(SywxYqAnnex.class).eq(SywxYqAnnex::getYqId,id));
    }
}
